// --------------------------------------------------
// Simple RISC calculator
//
// Operation:
//   1 = Addition
//   2 = Subtraction
//   3 = Multiplication
//   4 = Division
// --------------------------------------------------

/*   CODE WRITTEN BY AI

Input to AI:
Write a simple calculator program

*/

start:

addi x1, x0, 20       // First number
addi x2, x0, 5        // Second number
addi x3, x0, 3        // Select operation

addi x10, x0, 0       // Result
addi x11, x0, 0       // Remainder

cout << "SIMPLE RISC CALCULATOR" << endl;
cout << "First number  = " << x1 << endl;
cout << "Second number = " << x2 << endl;

// Test for addition

addi x4, x0, 1
beq  x3, x4, addition

// Test for subtraction

addi x4, x0, 2
beq  x3, x4, subtraction

// Test for multiplication

addi x4, x0, 3
beq  x3, x4, multiplication

// Test for division

addi x4, x0, 4
beq  x3, x4, division

// The operation was not valid


cout << "Invalid operation" << endl;
jal  x0, finished

// --------------------------------------------------
// Addition
// --------------------------------------------------

addition:
add  x10, x1, x2

cout << "Operation: Addition" << endl;
cout << x1 << " + " << x2 << " = " << x10 << endl;

jal  x0, finished

// --------------------------------------------------
// Subtraction
// --------------------------------------------------

subtraction:
sub  x10, x1, x2


cout << "Operation: Subtraction" << endl;
cout << x1 << " - " << x2 << " = " << x10 << endl;

jal  x0, finished

// --------------------------------------------------
// Multiplication using repeated addition
// --------------------------------------------------

multiplication:
addi x10, x0, 0       // Result = 0
add  x5, x2, x0       // Loop counter = second number

beq  x5, x0, multiply_done

multiply_loop:
add  x10, x10, x1     // Add first number to result
addi x5, x5, -1
bne  x5, x0, multiply_loop

multiply_done:

cout << "Operation: Multiplication" << endl;
cout << x1 << " * " << x2 << " = " << x10 << endl;

jal  x0, finished

// --------------------------------------------------
// Division using repeated subtraction
// --------------------------------------------------

division:
beq  x2, x0, divide_by_zero

addi x10, x0, 0       // Quotient = 0
add  x11, x1, x0      // Remainder = first number

division_loop:
blt  x11, x2, division_done
sub  x11, x11, x2
addi x10, x10, 1
jal  x0, division_loop

division_done:

cout << "Operation: Division" << endl;
cout << "Quotient  = " << x10 << endl;
cout << "Remainder = " << x11 << endl;

jal  x0, finished

divide_by_zero:

cout << "Error: Cannot divide by zero" << endl;

// --------------------------------------------------
// End of program
// --------------------------------------------------

finished:
cout << endl;
cout << "CALCULATION COMPLETE" << endl;
